<?php

namespace app\api\controller\shop;

use addons\epay\library\Service;
use app\admin\model\Article;
use app\admin\model\Articletype;
use app\admin\model\City;
use app\admin\model\Cmdlog;
use app\admin\model\Devicelock;
use app\admin\model\Roomclean;
use app\admin\model\Shop;
use app\admin\model\Shopcharge;
use app\admin\model\Shopchargeconfig;
use app\admin\model\Shopdevice;
use app\admin\model\Shopjoin;
use app\admin\model\Shoporder;
use app\admin\model\Shoproom;
use app\admin\model\Shoproombook;
use app\admin\model\Shoptableorder;
use app\admin\model\Shoptuan;
use app\admin\model\Tuanorder;
use app\admin\model\Userbalance;
use app\admin\model\Usertuan;
use app\admin\model\Userweixin;
use app\api\service\AirService;
use app\api\service\DeviceService;
use app\api\service\LockService;
use app\api\service\RoomService;
use app\api\service\TableService;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Http;
use fast\Random;
use think\Config;
use think\Validate;
use Yansongda\Pay\Pay;

/**
 */
class Cmd extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    public function openTableDoor()
    {
        $order_no = $this->request->param('order_no');
        $tableOrderModel = new Shoptableorder();
        $order = $tableOrderModel->where('order_no', $order_no)->where('status', 'doing')->find();
        if ($order) {

            $deviceModel = new Shopdevice();
            $device = $deviceModel->where('shop_id', $order['shop_id'])->where('room_id', -1)->find();
            if ($device) {
                $req = array();
                $req['orderNo'] = $order_no;
                $req['sn'] = $device['sn'];
                $req['sec'] = 60;
                $req['voice'] = "";

                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);

                $this->success('正在打开中');
            } else {
                $this->error("大门没有绑定设备");
            }
        } else {
            $this->error("此订单已不支持开门");
        }
    }

    public function openTable()
    {


        $user = $this->auth->getUser();
        $room_id = $this->request->param('room_id');
        $shop_id = $this->request->param('shop_id');
        $sec = $this->request->param('sec');
        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('shop_id', $shop_id)->where('room_id', $room_id)->where('device_type', 'tq')->find();


        $tableModel = new \app\admin\model\Shoptable();

        $order_no = getOrderSn();
        $id = 1;
        $tableOrderModel = new Shoptableorder();

        if ($sec == 3600) {

            $order = $tableOrderModel->where('status', 'doing')->where('table_id', $room_id)->find();
            if ($order) {
                $this->error("此台桌有用户正在使用,无法开台");
            }
            $shopModel = new Shop();
            $shop = $shopModel->where('id', $shop_id)->find();
            $table = $tableModel->where('id', $room_id)->find();
            $book_start = time();

            $db = array();
            $db['user_id'] = $user['id'];
            $db['table_id'] = $room_id;
            $db['shop_id'] = $shop['id'];
            $db['shop_name'] = $shop['name'];
            $db['table_name'] = $table['name'];
            $db['create_time'] = time();
            $db['order_no'] = $order_no;
            $db['amount'] = 0;
            $db['pay_amount'] = 0;
            $db['ya_amount'] = 0;
            $db['balance'] = 0;
            $db['status'] = 'doing';
            $db['book_start'] = $book_start;
            $db['org_id'] = 27;
            $db['hour_price'] = 0;
            $db['update_time'] = -1;
            $db['min'] = 60;
            $db['mt_code'] = '';
            $db['dou_code'] = '';
            $db['mark'] = '后台开台订单';
            $db['book_end'] = strtotime("+1 hour");

//            $id = $tableOrderModel->insertGetId($db);
//            if($id){
//                $tableModel->where('id',$room_id)->update(['status'=>'busy']);
//            }
        }
        $req = array();
        if ($device['version'] == 1) {
            $req = array();
            $req['switchOne'] = 1;
            $req['switchTwo'] = 1;
            $req['door'] = 1;
            $req['hello'] = "欢迎您祝您玩得开心";
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openTqCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        } else if ($device['version'] == 2) {
            $req = array();
            $req['sec'] = $sec;
            $req['voice'] = "您好开台成功,祝您玩得开心";
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openCmdUrl, json_encode($req));
        } else if ($device['version'] == 3) {
            $req = array();
            $req['sec'] = $sec;
            $req['orderId'] = $id;
            $req['deviceSn'] = $device['device_sn'];
            $req['orderNo'] = $id;

            $ret = http_post_json($this->tqV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);

        }


        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $user['id'], 'order_no' => $req['orderNo'], 'cmd' => json_encode($req), 'create_time' => time(), 'shop_id' => $shop_id, 'room_id' => $room_id]);


        $airService = new AirService();
        $airService->airCheck2($shop_id, $sec);
        $airService->banAir($room_id, 1);
        $this->success('操作成功');
    }

    public function closeTable()
    {


        $user = $this->auth->getUser();
        $room_id = $this->request->param('room_id');
        $shop_id = $this->request->param('shop_id');
        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('shop_id', $shop_id)->where('room_id', $room_id)->where('device_type', 'tq')->find();
        $tableModel = new \app\admin\model\Shoptable();
        $tableModel->where('id', $room_id)->update(['status' => 'free']);

        if ($device['version'] == 1) {
            $req = array();
            $req['switchOne'] = 0;
            $req['switchTwo'] = 0;
            $req['door'] = 2;
            $req['hello'] = "欢迎下次再玩";
            $req['orderNo'] = getOrderSn();
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openTqCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        } else if ($device['version'] == 2) {
            $req = array();
            $req['sec'] = 0;
            $req['voice'] = "欢迎下次光临，注意带好随身物品";
            $req['orderNo'] = getOrderSn();
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openCmdUrl, json_encode($req));
        } else if ($device['version'] == 3) {
            $req = array();
            $req['sec'] = 0;
            $req['orderId'] = 1;
            $req['orderNo'] = 1;
            $req['deviceSn'] = $device['device_sn'];

            $ret = http_post_json($this->tqV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }

        $airService = new AirService();
        $airService->banAir($room_id, 0);
        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $user['id'], 'order_no' => $req['orderNo'], 'cmd' => json_encode($req), 'create_time' => time(), 'shop_id' => $shop_id, 'room_id' => $room_id]);


        $this->success('');
    }

    //保洁端开台桌，不开空调
    public function openNewTable()
    {
        $user = $this->auth->getUser();
        $room_id = $this->request->param('room_id');
        $shop_id = $this->request->param('shop_id');
        $sec = $this->request->param('sec');
        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('shop_id', $shop_id)->where('room_id', $room_id)->where('device_type', 'tq')->find();

        $order_no = -4;//为保洁端开台的标志
        $id = 1;

        $req = array();
        if ($device['version'] == 1) {
            $req['switchOne'] = 1;
            $req['switchTwo'] = 1;
            $req['door'] = 1;
            $req['hello'] = "欢迎您祝您玩得开心";
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openTqCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        } else if ($device['version'] == 2) {
            $req['sec'] = $sec;
            $req['voice'] = "您好开台成功,祝您玩得开心";
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];

            $ret = http_post_json($this->openCmdUrl, json_encode($req));
        } else if ($device['version'] == 3) {
            $req['sec'] = $sec;
            $req['orderId'] = $id;
            $req['deviceSn'] = $device['device_sn'];
            $req['orderNo'] = $order_no;

            $ret = http_post_json($this->tqV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);

        }

        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $user['id'], 'order_no' => $req['orderNo'], 'cmd' => json_encode($req), 'create_time' => time(), 'shop_id' => $shop_id, 'room_id' => $room_id]);

//        $airService = new AirService();
//        $airService->airCheck2($shop_id, $sec);
//        $airService->banAir($room_id, 1);
        $this->success('操作成功');
    }

    /**
     * 开大门
     */
    public function openNewDoor()
    {

        $shop_id = $this->request->param('shop_id');
        $door = $this->request->param('door');
        $shopDeviceModel = new Shopdevice();
        $list = $shopDeviceModel->where('shop_id', $shop_id)->where('room_id', "<", 0)->select();
        error_log("===openNewDoor===" . json_encode($list));
        if (count($list) == 1) {

            $user = $this->auth->getUser();

            $device = $list[0];
            $order_no = getOrderSn();
            $db = array();
            $db['order_no'] = $order_no;
            $db['shop_id'] = $shop_id;
            $db['room_id'] = -1;
            $db['user_id'] = $user['id'];
            $db['create_time'] = time();


            $req = array();
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];
            $req['sec'] = 60;
            $req['voice'] = "";
            $db['cmd'] = json_encode($req);
            $cmdModel = new Cmdlog();


            $ret = http_post_json($this->openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            $cmdModel->insert($db);

        } else {
            //有前门和后门
            // -1 代表前门 也是大门 -2 代表后门
            if ($door == "") {
                $this->success('', '2');
            } else {
                if ($door == -1) {

                    $user = $this->auth->getUser();

                    $device = $shopDeviceModel->where('shop_id', $shop_id)->where('room_id', -1)->find();
                    $order_no = getOrderSn();
                    $db = array();
                    $db['order_no'] = $order_no;
                    $db['shop_id'] = $shop_id;
                    $db['room_id'] = -1;
                    $db['user_id'] = $user['id'];
                    $db['create_time'] = time();


                    $req = array();
                    $req['orderNo'] = $order_no;
                    $req['sn'] = $device['sn'];
                    $req['sec'] = 60;
                    $req['voice'] = "";
                    $db['cmd'] = json_encode($req);
                    $cmdModel = new Cmdlog();


                    $ret = http_post_json($this->openCmdUrl, json_encode($req));
                    $ret = json_decode($ret, true);
                    $cmdModel->insert($db);
                } else {


                    $user = $this->auth->getUser();

                    $device = $shopDeviceModel->where('shop_id', $shop_id)->where('room_id', -2)->find();
                    $order_no = getOrderSn();
                    $db = array();
                    $db['order_no'] = $order_no;
                    $db['shop_id'] = $shop_id;
                    $db['room_id'] = -2;
                    $db['user_id'] = $user['id'];
                    $db['create_time'] = time();


                    $req = array();
                    $req['orderNo'] = $order_no;
                    $req['sn'] = $device['sn'];
                    $req['sec'] = 60;
                    $req['voice'] = "";

                    $db['cmd'] = json_encode($req);
                    $cmdModel = new Cmdlog();
                    $cmdModel->insert($db);

                    $ret = http_post_json($this->openCmdUrl, json_encode($req));
                    $ret = json_decode($ret, true);

                }

                $this->success('', '已打开');

            }


        }
    }

    /**
     * 开包厢大门
     */
    public function openNewRoom()
    {

        $shop_id = $this->request->param('shop_id');
        $room_id = $this->request->param('room_id');
        $sec = $this->request->param('sec');

        $shopDeviceModel = new Shopdevice();
        $device = $shopDeviceModel->where('shop_id', $shop_id)->where('device_type', 'qp')->where('room_id', $room_id)->find();

        $user = $this->auth->getUser();
        $cmdModel = new Cmdlog();
        $shopRoomModel = new Shoproom();

        if ($sec == 0) {

            $order_no = getOrderSn();
            $shopRoomModel->where('id', $room_id)->update(['status' => 'free']);

        } else {
            $order_no = getOrderSn();

        }

        if ($sec == 3600) {


//            $time1 = time();
//            $time2 = strtotime("+1 hour");
//
//            $isBooked = -1;
//            $shopRoomBookModel = new Shoproombook();
//            $bookList = $shopRoomBookModel->where('room_id',$room_id)->where('status','in',['undo','doing'])->order('start_time','desc')->select();
//            foreach ($bookList as $key=>$val){
//                $start = $val['start_time'];
//                $end = $val['end_time'];
//                if($time1 <=$start &&  $time2 >= $end) $isBooked = 1;//全包围
//                if($time1 >= $start && $time2 <= $end) $isBooked = 1;//在区间范围内
//                if($time1 <= $end && $time2 >$end) $isBooked = 1; //
//                if($time1 < $start && $time2 <=$end && $time2 >= $start) $isBooked = 1;
//
//            }
//            if($isBooked == 1) $this->error("此时间段已被预定");
//            //插入数据库订单
//
//            $shopModel = new Shop();
//            $shop = $shopModel->where('id',$shop_id)->find();
//            $room = $shopRoomModel->where('id',$room_id)->find();
//
//
//            $db['shop_id'] = $shop_id;
//            $db['shop_name'] = $shop['name'];
//            $db['room_id'] = $room_id;
//            $db['room_name'] = $room['name'];
//            $db['user_id'] = $user['id'];
//            $db['order_no'] = $order_no;
//            $db['create_time'] = time();
//            $db['update_time'] = time();
//            $db['amount'] = 0;
//            $db['pay_amount'] = 0;
//            $db['book_start'] = $time1;
//            $db['book_end'] = $time2;
//            $db['set_id'] = "";
//            $db['tuan_id'] = "";
//            $db['balance'] = "";
//            $db['mt_code'] = "";
//            $db['dou_code'] = "";
//            $db['dou_token'] = "";
//            $db['org_id'] = 27;
//            $db['pay_rate'] = 0;
//            $db['rate'] = 0;
//            $db['coupon_id'] = "";
//            $db['open_code'] = "";
//            $db['mark'] = "后台包厢开门订单";
//
//            $shopOrderModel = new Shoporder();
//            $id = $shopOrderModel->insertGetId($db);
//            if($id){
//                $order = $shopOrderModel->where('id',$id)->find();
//                $roomService = new RoomService();
//                $roomService->addBookData($order);
//                $shopRoomModel->where('id',$room_id)->update(['status'=>'busy']);
//                $shopOrderModel->where('id', $id)->update(['status' => 'doing', 'update_time' => time()]);
//
//            }
        }

        $db = array();
        $db['order_no'] = $order_no;
        $db['shop_id'] = $shop_id;
        $db['room_id'] = $room_id;
        $db['user_id'] = $user['id'];
        $db['create_time'] = time();

        $req = array();
        if ($device['version'] == 1) {
            $req['orderNo'] = $order_no;
            $req['sn'] = $device['sn'];
            $req['sec'] = $sec;
            $req['voice'] = "";
            $ret = http_post_json($this->openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
        } else {
            $req['sec'] = $sec;
            $req['orderId'] = 1;
            $req['deviceSn'] = $device['device_sn'];
            $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }
        $db['cmd'] = json_encode($req);
        $cmdModel->insert($db);

        $ret = array();
        $ret['is_bt'] = $device['is_bt'];
        $ret['room_id'] = $device['room_id'];
        $ret['shop_id'] = $device['shop_id'];

        $deviceLockModel = new Devicelock();
        $lock = $deviceLockModel->where('room_id', $device['room_id'])->find();
        if ($lock) {
            if ($lock['is_gateway'] == 1) {
                //网关开锁
                $lockService = new LockService();
                $lockReq = array();
                $lockReq['lockId'] = $lock['lockId'];
                $lockService->openLock($lockReq);

            }
        }

        $this->success('发送成功', $ret);
    }


    /**
     * 关闭订单
     */
    public function closeOrder()
    {

        $order_no = $this->request->param('order_no');
        $shopOrderModel = new Shoporder();
        $shopRoomBookModel = new Shoproombook();
        $roomCleanModel = new Roomclean();
        $order = $shopOrderModel->where('order_no', $order_no)->find();
        if ($order['status'] == 'done' || $order['status'] == 'refund') {
            $this->success('此订单已结束 无需关闭');
        }
        $shopOrderModel->where('id', $order['id'])->update(['status' => 'done']);
        $book = $shopRoomBookModel->where('order_id', $order['id'])->find();
        if ($book) {
            if ($book['status'] != 'done') {
                $shopRoomBookModel->where('id', $book['id'])->update(['status' => 'done']);
            }
        }
        $shopRoomModel = new Shoproom();
        $shopRoomModel->where('id', $book['room_id'])->update(['status' => 'free']);
        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('room_id', $order['room_id'])->find();

        $user = $this->auth->getUser();
        $user_id = $user['id'];
        $req = array();

        if ($device['version'] == 1) {
            $req['orderNo'] = $order['order_no'];
            $req['sn'] = $device['sn'];
            $req['sec'] = 0;
            $req['voice'] = "";
            $ret = http_post_json($this->openCmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
        } else {
            $req = array();
            $req['sec'] = 0;
            $req['orderId'] = $order['id'];
            $req['deviceSn'] = $device['device_sn'];
            $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
            $ret = json_decode($ret, true);
            if ($ret['code'] == 0) {
                $msg = "正在打开中";
            }
        }


        $shopRoomModel->where('id', $order['room_id'])->update(['status' => 'free', 'is_clean' => -1]);

        $cmdModel = new Cmdlog();
        $cmdModel->insert(['user_id' => $user_id, 'cmd' => json_encode($req),
            'create_time' => time(), 'shop_id' => $book['shop_id'], 'room_id' => $book['room_id']]);

        $start = time();
        $endTime = strtotime("+30 minute", time());

        $roomCleanModel->where('order_id', $book['order_id'])->update(['start_time' => $start, 'end_time' => $endTime]);

        $this->success('已关闭');
    }

    /**
     * 开门
     */
    public function openDoor()
    {
        $user = $this->auth->getUser();
        $order_no = $this->request->param('order_no');
        $type = $this->request->param('type');
        //订单列表点击打开
        $shopOrderModel = new Shoporder();
        $order = $shopOrderModel->where('order_no', $order_no)->find();
        $room_id = $order['room_id'];
        $shop_id = $order['shop_id'];
        $sec = 0;
        if ($order['status'] == 'done') {
            $this->error("订单状态 不允许开启");
        }
        if ($order['status'] == 'payed') {
            $time = strtotime("+10 minute", time());
            if ($order['book_start'] > $time || $time > $order['book_end']) {
                $this->error("订单开始和结束 不在当前时间范围内");
            } else {
                $minute = floor(($order['book_end'] - time()) % 86400 / 60);
                $sec = $minute * 60;
            }
            $shopOrderModel->where('id', $order['id'])->update(['status' => 'doing']);
        }

        if ($order['status'] == 'doing') {
            $minute = floor(($order['book_end'] - time()) % 86400 / 60);
            $sec = $minute * 60;
        }

        $deviceModel = new Shopdevice();

        if ($type == 'door') {
            $device = $deviceModel->where('shop_id', $shop_id)->where('room_id', -1)->find();
            //发送指令
//            if ($device['status'] != 'online') {
//                $this->error("设备没在线，请联系客服");
//            }
            $orderNo = getOrderSn();
            $sn = $device['sn'];
            $req = array();


            if ($device['version'] == 1) {

                $req['sec'] = 20;
                $req['orderNo'] = $orderNo;
                $req['sn'] = $sn;
                $req['voice'] = "";
                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            } else {
                $req = array();
                $req['sec'] = 20;
                $req['orderId'] = 1;
                $req['deviceSn'] = $device['device_sn'];
                $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            }
            $cmdModel = new Cmdlog();
            $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req),
                'create_time' => time(), 'order_no' => $order_no, 'shop_id' => $shop_id, 'room_id' => -1]);

            $this->success("正在打开");

        } else {


            $shopRoomModel = new Shoproom();
            if ($sec == 0) {
                $status = "free";
            } else {
                $status = "busy";
            }
            $room = $shopRoomModel->where('id', $room_id)->find();
            if ($room['status'] == 'stop') {
                $this->error("店铺设置维护中，不可开门");
            } else {

                $shopRoomModel->where('id', $room_id)->update(['status' => $status]);

            }


            $device = $deviceModel->where('shop_id', $shop_id)->where('device_type', 'qp')->where('room_id', $room_id)->find();
            //发送指令
//            if ($device['status'] != 'online') {
//                $this->error("设备没在线，请联系客服");
//            }

            $sn = $device['sn'];
            $req = array();
            $req['sec'] = $sec;
            $req['orderNo'] = $order_no;
            $req['sn'] = $sn;
            $req['voice'] = "";

            if ($device['version'] == 1) {
                $ret = http_post_json($this->openCmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                } else {
                    $msg = "发送指令失败";

                }

                $deviceLockModel = new Devicelock();
                $lock = $deviceLockModel->where('room_id', $device['room_id'])->find();


                if ($lock) {
                    if ($lock['is_gateway'] == 1) {
                        //网关开锁
                        $lockService = new LockService();
                        $lockReq = array();
                        $lockReq['lockId'] = $lock['lockId'];
                        $lockService->openLock($lockReq);

                    }
                }

            } else {
                $req = array();
                $req['sec'] = $sec;
                $req['orderId'] = $order['id'];
                $req['deviceSn'] = $device['device_sn'];
                $ret = http_post_json($this->openV3CmdUrl, json_encode($req));
                $ret = json_decode($ret, true);
                if ($ret['code'] == 0) {
                    $msg = "正在打开中";
                }
            }


            $cmdModel = new Cmdlog();

            $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req),
                'create_time' => time(), 'order_no' => $order_no, 'shop_id' => $shop_id, 'room_id' => $room_id]);

            $this->success("成功");
        }


    }

    /**
     * 续时
     */
    public function xuOrder()
    {

        $user = $this->auth->getUser();
        $order_no = $this->request->param('order_no');
        $hour = $this->request->param('hour');

        $shopOrderModel = new Shoporder();
        $order = $shopOrderModel->where('order_no', $order_no)->find();
        if ($order['status'] == 'done' || $order['status'] == 'refund') {
            $this->error("此订单，不可续费");
        }

        $sec = $hour * 60 * 60;
        $deviceModel = new Shopdevice();
        $device = $deviceModel->where('shop_id', $order['shop_id'])->where('room_id', $order['room_id'])->find();
        //发送指令
        if ($device['status'] != 'online') {
            $this->error("设备没在线，请联系客服");
        }

        $endTime = strtotime("+" . ($hour * 60) . " minutes", $order['book_end']);

        $orderNo = getOrderSn();
        $roomBookModel = new Shoproombook();
        $startTime = $order['book_start'];
        $checkStart = $roomBookModel->where('room_id', $order['room_id'])->where('start_time', "<=", $startTime)->where("end_time", ">=", $endTime)->find();
        $checkEnd = $roomBookModel->where('room_id', $order['room_id'])->where('start_time', "<=", $endTime)->where("end_time", ">=", $endTime)->find();

        if ($checkStart != null || $checkEnd != null) {

            if ($checkStart['id'] != $order['room_id'] || $checkEnd['id'] == $order['room_id']) {
                $this->error("此时间段预被预定,无法续费");
            }
        }

        $shopOrderModel->where('id', $order['id'])->update(['order_no' => $orderNo, 'book_end' => $endTime]);
        $roomBookModel->where('order_id', $order['id'])->update(['end_time' => $endTime]);
        $sn = $device['sn'];
        $req = array();
        $req['sec'] = $sec;
        $req['orderNo'] = $orderNo;
        $req['sn'] = $sn;
        $req['voice'] = "";
        $cmdModel = new Cmdlog();


        $ret = http_post_json($this->openCmdUrl, json_encode($req));
        $ret = json_decode($ret, true);
        if ($ret['code'] == 0) {
            $msg = "正在打开中";
        } else {
            $msg = "发送指令失败";

        }
        $cmdModel->insert(['user_id' => $user['id'], 'cmd' => json_encode($req),
            'create_time' => time(), 'order_no' => $order_no, 'shop_id' => $order['shop_id'], 'room_id' => $order['room_id']]);


        $this->success('续时成功');

    }


    public function openAir()
    {

        $sec = $this->request->param('sec');
        $shop_id = $this->request->param('shop_id');
        $airService = new AirService();
        if ($sec == -1) {
            //打开
            $airService->closeAir($shop_id, 1);
        } else if ($sec == -2) {
            //关闭
            $airService->closeAir($shop_id, 0);
        } else {
            $deviceModel = new Shopdevice();
            $device = $deviceModel->where('shop_id', $shop_id)->where('type', 'air')->find();
            if ($device) {
                $airService = new AirService();
                $airService->airCheck3($device['sn'], $sec);
                $this->success('成功');
            } else {
                $this->error("没有配置空调");
            }
        }

    }
}
